const { PrismaClient } = require('@prisma/client');

const prisma = new PrismaClient();

async function checkDatabase() {
  try {
    console.log('检查用户表...');
    const users = await prisma.user.findMany();
    console.log('用户数据:', users);

    if (users.length === 0) {
      console.log('用户表为空，创建默认用户...');
      const defaultUser = await prisma.user.create({
        data: {
          username: 'admin',
          email: 'admin@example.com',
          password: 'hashed_password', // 这里应该是哈希后的密码
        }
      });
      console.log('创建的默认用户:', defaultUser);
    }

    console.log('\n检查数据库结构...');
    // 检查外键约束
    const result = await prisma.$queryRaw`
      SELECT
        TABLE_NAME,
        COLUMN_NAME,
        CONSTRAINT_NAME,
        REFERENCED_TABLE_NAME,
        REFERENCED_COLUMN_NAME
      FROM
        INFORMATION_SCHEMA.KEY_COLUMN_USAGE
      WHERE
        REFERENCED_TABLE_SCHEMA = 'tiktok_content_sys'
        AND TABLE_NAME = 'extraction_tasks'
    `;
    console.log('外键约束:', result);

    // 尝试创建一个测试任务
    console.log('\n尝试创建测试任务...');
    try {
      const testTask = await prisma.extractionTask.create({
        data: {
          name: '测试任务',
          keywords: '["测试"]',
          maxCount: 5,
          createdBy: 1
        }
      });
      console.log('测试任务创建成功:', testTask);

      // 删除测试任务
      await prisma.extractionTask.delete({
        where: { id: testTask.id }
      });
      console.log('测试任务已删除');
    } catch (createError) {
      console.error('创建测试任务失败:', createError);
    }

  } catch (error) {
    console.error('错误:', error);
  } finally {
    await prisma.$disconnect();
  }
}

checkDatabase();
